package com.easyprogram.notice.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.easyprogram.entity.PageResult;
import com.easyprogram.entity.Result;
import com.easyprogram.entity.StatusCode;
import com.easyprogram.notice.pojo.Notice;
import com.easyprogram.notice.pojo.NoticeFresh;
import com.easyprogram.notice.service.NoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/notice")
@CrossOrigin
public class NoticeController {
    @Autowired
    private NoticeService noticeService;

    // GET /notice/{id} 根据id查询消息通知
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public Result selectById(@PathVariable("id") String id){
        Notice notice = noticeService.selectById(id);
        return new Result(true, StatusCode.OK, "查询成功", notice);
    }

    // POST /notice/search/{page}/{size} 根据条件分页查询消息通知
    @RequestMapping(value = "/search/{page}/{size}", method = RequestMethod.POST)
    public Result selectByPage(@RequestBody Notice notice,
                               @PathVariable("page") Integer page,
                               @PathVariable("size") Integer size){
        Page<Notice> pageData = noticeService.selectByPage(notice, page, size);
        PageResult<Notice> pageResult = new PageResult<>(pageData.getTotal(), pageData.getPages(), pageData.getRecords());
        return new Result(true, StatusCode.OK, "查询成功", pageResult);
    }

    // POST /notice  新增通知
    @RequestMapping(method = RequestMethod.POST)
    public Result save(@RequestBody Notice notice){
        noticeService.save(notice);
        return new Result(true, StatusCode.OK, "新增通知成功");
    }

    // PUT /notice 修改通知
    @RequestMapping(method = RequestMethod.PUT)
    public Result updateById(@RequestBody Notice notice){
        noticeService.updateById(notice);
        return new Result(true, StatusCode.OK, "修改成功");
    }

    // DELETE /notice 删除通知
    @RequestMapping(method = RequestMethod.DELETE)
    public Result noticeDelete(@RequestBody Notice notice){
        noticeService.noticeDelete(notice);
        return new Result(true, StatusCode.OK, "删除成功");
    }

    // GET /notice/fresh/{userId}/{page}/{size} 根据用户id查询该用户的待推送消息（新消息）
    @RequestMapping(value = "/fresh/{userId}/{page}/{size}", method = RequestMethod.GET)
    public Result freshPage(@PathVariable("userId") String userId,
                            @PathVariable("page") Integer page,
                            @PathVariable("size") Integer size){
        Page<NoticeFresh> pageData = noticeService.freshPage(userId, page, size);
        PageResult<NoticeFresh> pageResult = new PageResult<>(pageData.getTotal(), pageData.getPages(), pageData.getRecords());
        return new Result(true, StatusCode.OK, "查询成功", pageResult);
    }

    // DELETE /notice/fresh 删除待推送消息（新消息）
    @RequestMapping(value = "/fresh", method = RequestMethod.DELETE)
    public Result freshDelete(@RequestBody NoticeFresh noticeFresh){
        noticeService.freshDelete(noticeFresh);
        return new Result(true, StatusCode.OK, "删除成功");
    }
}
